import os
"""  k4trash split module
 *  Copyright (c) 2008 Dmitry K <drgolova@gmail.com>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Library General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
 """
def split(__filename,__numchunks):
	abc='abcdefghijklmnopqrstuvwxys'
        try:
            f = open(__filename, 'rb')
        except (OSError, IOError), e:
            raise FileSplitterException, str(e)

        bname = (os.path.split(__filename))[1]
        fsize = os.path.getsize(__filename)
        __chunksize = int(float(fsize)/float(__numchunks))

        chunksz = __chunksize
        total_bytes = 0

        for x in range(__numchunks):
            chunkfilename = 'xa'+abc[x]

            if x == __numchunks - 1:
                chunksz = fsize - total_bytes

            try:
                data = f.read(chunksz)
                total_bytes += len(data)
                chunkf = file(chunkfilename, 'wb')
                chunkf.write(data)
                chunkf.close()
            except (OSError, IOError), e:
                print e
                continue
            except EOFError, e:
                print e
                break